package topcbl.taxi;

import java.util.ArrayList;

import topcbl.adapter.AmountOfTaxiAdapter;
import topcbl.data.Car;
import topcbl.data.SpinnerModel;
import topcbl.task.GetNearCarTask;
import topcbl.taxi.CarTypeDialog.OnChooseCarTypeListener;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.model.LatLng;

public class CallTaxiActivity extends FragmentActivity {

	public ArrayList<SpinnerModel> CustomListViewValuesArr = new ArrayList<SpinnerModel>();
	AmountOfTaxiAdapter adapter;
	CallTaxiActivity activity = null;
	public ArrayList<String> NumberOfCar = new ArrayList<String>();
	ArrayAdapter<String> adapterNumberOfCar = null;
	String[] amount = { "5 xe", "6 xe", "7 xe", "8 xe", "9 xe", "10 xe" };
	ImageView image;
	Animation animation;
	CallTaxiMapFragment mapfragment;

	/***** Post Parameter *****/
	String username = "";
	LatLng position;
	int numbercar = 5;
	int distance = 99999;
	int cartype = 0;
	double lat, lng;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_calltaxi);
		activity = this;
		mapfragment = new CallTaxiMapFragment();
		// Map
		FragmentManager fragment_manager = getSupportFragmentManager();
		if (savedInstanceState == null) {
			fragment_manager.beginTransaction()
					.replace(R.id.content_frame, mapfragment).commit();
		}
		image = (ImageView) findViewById(R.id.imageView1);
		Intent receiveIntent = getIntent();
		lat = receiveIntent.getDoubleExtra("lat", 0);
		lng = receiveIntent.getDoubleExtra("lng", 0);
		animation = AnimationUtils.loadAnimation(getApplicationContext(),
				R.anim.clockwise);
		findNearCars();

		// init choose number of cars spinner
		Spinner spinnerChooseNumberOfCar = (Spinner) findViewById(R.id.spinnerNumOfCar);
		setListData();
		Resources res = getResources();
		adapter = new AmountOfTaxiAdapter(activity, R.layout.row_spinner,
				CustomListViewValuesArr, res);
		spinnerChooseNumberOfCar.setAdapter(adapter);
		spinnerChooseNumberOfCar
				.setOnItemSelectedListener(new OnItemSelectedListener() {
					@Override
					public void onItemSelected(AdapterView<?> parentView,
							View v, int position, long id) {
						// Get selected row data to show on screen
						String strNumberOfCar = ((TextView) v
								.findViewById(R.id.tvNumOfCar)).getText()
								.toString();
						String OutputMsg = getString(R.string.toastShowNumberOfCar)
								+ " " + strNumberOfCar;
						Toast.makeText(getApplicationContext(), OutputMsg,
								Toast.LENGTH_SHORT).show();
						numbercar = Integer.parseInt(strNumberOfCar.split(" ")[0]);
						findNearCars();
					}

					@Override
					public void onNothingSelected(AdapterView<?> parentView) {
						// your code here
					}
				});

		// Button Choose Taxi Car Type
		ImageButton ibtnCarType = (ImageButton) findViewById(R.id.ibtnAllCarType);
		ibtnCarType.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				CarTypeDialog dialog = new CarTypeDialog(CallTaxiActivity.this,
						R.style.Theme_AlertDialog);
				dialog.setOnChooseCarTypeListener(new OnChooseCarTypeListener() {

					@Override
					public void onChoose(int result) {
						cartype = result;
						if (cartype == 0)
							Toast.makeText(getApplicationContext(),
									"Tìm tất cả các loại xe taxi",
									Toast.LENGTH_SHORT).show();
						else
							Toast.makeText(getApplicationContext(),
									"Chọn xe " + cartype + " chỗ",
									Toast.LENGTH_SHORT).show();
						findNearCars();
					}
				});
				dialog.show();
			}
		});
	}

	/****** Function to set data in ArrayList *************/
	public void setListData() {

		for (int i = 5; i < 11; i++) {

			final SpinnerModel sched = new SpinnerModel();

			/******* Firstly take data in model object ******/
			sched.setNumberText(i + " xe");
			sched.setImage("image" + i);
			sched.setUrl(i + " xe");

			/******** Take Model Object in ArrayList **********/
			CustomListViewValuesArr.add(sched);
		}

	}

	String getUrl() {
		return "http://gps.smartsafe.vn/taxiws/Service1.svc/getNearCars?username="
				+ username
				+ "&myLat="
				+ position.latitude
				+ "&myLon="
				+ position.longitude
				+ "&NumberCar="
				+ numbercar
				+ "&NumberKm="
				+ distance + "&CarType=" + cartype;
	}

	public void takeNearCars(ArrayList<Car> list) {
		mapfragment.addAllTaxiOnMap(list);
		image.clearAnimation();
		image.setVisibility(View.GONE);
	}

	public void findNearCars() {
		image.setVisibility(View.VISIBLE);
		image.startAnimation(animation);
		new GetNearCarTask(CallTaxiActivity.this, lat, lng, numbercar,
				distance, cartype).execute();
	}
}
